/************************************************************************************/
/*  Program name CAPAPEXT_INV_D.sql                                                 */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
set verify off;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPEXT_INV_D';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPAP_INV_D.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_B (p_NGO VARCHAR2) IS
	SELECT
		A.*,
		F.Invoice_Num	Invoice_Num,
		H.Batch_Name	Batch_Name,
		H.Batch_Date	Batch_Date,
		B.Name			OU_Name
	FROM
		AP_INVOICE_DISTRIBUTIONS_ALL	A,
		AP_INVOICES_ALL					F,
		AP_BATCHES_ALL					H,
		HR_ALL_ORGANIZATION_UNITS		B,
		HR_ORGANIZATION_INFORMATION		I
	WHERE	SUBSTR(B.Name,4,4)			=	p_NGO
	AND		A.Invoice_ID				=	F.Invoice_ID
	AND		F.Batch_ID					=	H.Batch_ID --(+)
	AND		B.Organization_ID			=	F.Org_ID
	AND		B.Organization_ID			=	I.Organization_ID
	AND		I.Org_Information_Context	=	'Operating Unit Information'
	ORDER BY
		A.Batch_ID,
		A.Invoice_ID,
		A.Distribution_Line_Number;

	lv_PO_Num		VARCHAR2(20);
	lv_PO_LineNum	NUMBER;
	lv_PO_DistNum	NUMBER;

	lv_RCV_OrgName	VARCHAR2(240);
	lv_RCV_RecNum	VARCHAR2(30);
	lv_RCV_Qty		NUMBER;

BEGIN
	lv_NGO		:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_B(lv_NGO) LOOP

			lv_PO_Num		:=	NULL;
			lv_PO_LineNum	:=	NULL;
			lv_PO_DistNum	:=	NULL;
			lv_RCV_OrgName	:=	NULL;
			lv_RCV_RecNum	:=	NULL;
			lv_RCV_Qty		:=	NULL;

			CAPBREXT_COMMON.GetPOInfo(C1.PO_Distribution_ID,lv_PO_Num,lv_PO_LineNum,lv_PO_DistNum);
			CAPBREXT_COMMON.GetRCVTransInfo(C1.RCV_Transaction_ID,lv_RCV_OrgName,lv_RCV_RecNum,lv_RCV_Qty);

			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Accounting_Date||CHR(9)||
				C1.Accrual_Posted_Flag||CHR(9)||
				C1.Assets_Addition_Flag||CHR(9)||
				C1.Assets_Tracking_Flag||CHR(9)||
				C1.Cash_Posted_Flag||CHR(9)||
				C1.Distribution_Line_Number||CHR(9)||
					C1.Dist_Code_Combination_ID||CHR(9)||
					C1.Invoice_ID||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
				C1.Line_Type_Lookup_Code||CHR(9)||
				C1.Period_Name||CHR(9)||
					C1.Set_Of_Books_ID||CHR(9)||
				C1.Accts_Pay_Code_Combination_ID||CHR(9)||		-- ???
				C1.Amount||CHR(9)||
				C1.Base_Amount||CHR(9)||
				C1.Base_Invoice_Price_Variance||CHR(9)||
					C1.Batch_ID||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.Creation_Date||CHR(9)||
				REPLACE(C1.Description,CHR(10),'^C10^')||CHR(9)||
				C1.Exchange_Rate_Variance||CHR(9)||
				C1.Final_Match_Flag||CHR(9)||
				C1.Income_Tax_Region||CHR(9)||
				C1.Invoice_Price_Variance||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
				C1.Match_Status_Flag||CHR(9)||
				C1.Posted_Flag||CHR(9)||
					C1.PO_Distribution_ID||CHR(9)||
				C1.Program_Application_ID||CHR(9)||
				C1.Program_ID||CHR(9)||
				C1.Program_Update_Date||CHR(9)||
				C1.Quantity_Invoiced||CHR(9)||
				C1.Rate_Var_Code_Combination_ID||CHR(9)||
				C1.Request_ID||CHR(9)||
				C1.Reversal_Flag||CHR(9)||
				C1.Type_1099||CHR(9)||
				C1.Unit_Price||CHR(9)||
				C1.Vat_Code||CHR(9)||
				C1.Amount_Encumbered||CHR(9)||
				C1.Base_Amount_Encumbered||CHR(9)||
				C1.Encumbered_Flag||CHR(9)||
				C1.Exchange_Date||CHR(9)||
				C1.Exchange_Rate||CHR(9)||
				C1.Exchange_Rate_Type||CHR(9)||
				C1.Price_Adjustment_Flag||CHR(9)||
				C1.Price_Var_Code_Combination_ID||CHR(9)||
				C1.Quantity_Unencumbered||CHR(9)||
				C1.Stat_Amount||CHR(9)||
				C1.Amount_To_Post||CHR(9)||
				C1.Attribute1||CHR(9)||
				C1.Attribute10||CHR(9)||
				C1.Attribute11||CHR(9)||
				C1.Attribute12||CHR(9)||
				C1.Attribute13||CHR(9)||
				C1.Attribute14||CHR(9)||
				C1.Attribute15||CHR(9)||
				C1.Attribute2||CHR(9)||
				C1.Attribute3||CHR(9)||
				C1.Attribute4||CHR(9)||
				C1.Attribute5||CHR(9)||
				C1.Attribute6||CHR(9)||
				C1.Attribute7||CHR(9)||
				C1.Attribute8||CHR(9)||
				C1.Attribute9||CHR(9)||
				C1.Attribute_Category||CHR(9)||
				C1.Base_Amount_To_Post||CHR(9)||
				C1.Cash_Je_Batch_ID||CHR(9)||
				C1.Expenditure_Item_Date||CHR(9)||
				C1.Expenditure_Organization_ID||CHR(9)||
				C1.Expenditure_Type||CHR(9)||
				C1.JE_Batch_ID||CHR(9)||
					C1.Parent_Invoice_ID||CHR(9)||
				C1.PA_Addition_Flag||CHR(9)||
				C1.PA_Quantity||CHR(9)||
				C1.Posted_Amount||CHR(9)||
				C1.Posted_Base_Amount||CHR(9)||
				C1.Prepay_Amount_Remaining||CHR(9)||
				C1.Project_Accounting_Context||CHR(9)||
				C1.Project_ID||CHR(9)||
				C1.Task_ID||CHR(9)||
				C1.USSGL_Transaction_Code||CHR(9)||
				C1.USSGL_Trx_Code_Context||CHR(9)||
				C1.Earliest_Settlement_Date||CHR(9)||
				C1.Req_Distribution_ID||CHR(9)||
				C1.Quantity_Variance||CHR(9)||
				C1.Base_Quantity_Variance||CHR(9)||
					C1.Packet_ID||CHR(9)||				-- ???
				C1.AWT_Flag||CHR(9)||
				C1.AWT_Group_ID||CHR(9)||
				C1.AWT_Tax_Rate_ID||CHR(9)||
				C1.AWT_Gross_Amount||CHR(9)||
				C1.AWT_Invoice_ID||CHR(9)||
				C1.AWT_Origin_Group_ID||CHR(9)||
				C1.Reference_1||CHR(9)||
				C1.Reference_2||CHR(9)||
					C1.Org_ID||CHR(9)||
				C1.Other_Invoice_ID||CHR(9)||
				C1.AWT_Invoice_Payment_ID||CHR(9)||
				C1.Global_Attribute_Category||CHR(9)||
				C1.Global_Attribute1||CHR(9)||
				C1.Global_Attribute2||CHR(9)||
				C1.Global_Attribute3||CHR(9)||
				C1.Global_Attribute4||CHR(9)||
				C1.Global_Attribute5||CHR(9)||
				C1.Global_Attribute6||CHR(9)||
				C1.Global_Attribute7||CHR(9)||
				C1.Global_Attribute8||CHR(9)||
				C1.Global_Attribute9||CHR(9)||
				C1.Global_Attribute10||CHR(9)||
				C1.Global_Attribute11||CHR(9)||
				C1.Global_Attribute12||CHR(9)||
				C1.Global_Attribute13||CHR(9)||
				C1.Global_Attribute14||CHR(9)||
				C1.Global_Attribute15||CHR(9)||
				C1.Global_Attribute16||CHR(9)||
				C1.Global_Attribute17||CHR(9)||
				C1.Global_Attribute18||CHR(9)||
				C1.Global_Attribute19||CHR(9)||
				C1.Global_Attribute20||CHR(9)||
				C1.Amount_Includes_Tax_Flag||CHR(9)||
				C1.Tax_Calculated_Flag||CHR(9)||
				C1.Line_Group_Number||CHR(9)||
				C1.Receipt_Verified_Flag||CHR(9)||
				C1.Receipt_Required_Flag||CHR(9)||
				C1.Receipt_Missing_Flag||CHR(9)||
				C1.Justification||CHR(9)||
				C1.Expense_Group||CHR(9)||
				C1.Start_Expense_Date||CHR(9)||
				C1.End_Expense_Date||CHR(9)||
				C1.Receipt_Currency_Code||CHR(9)||
				C1.Receipt_Conversion_Rate||CHR(9)||
				C1.Receipt_Currency_Amount||CHR(9)||
				C1.Daily_Amount||CHR(9)||
				C1.WEB_Parameter_ID||CHR(9)||
				C1.Adjustment_Reason||CHR(9)||
				C1.Award_ID||CHR(9)||
				C1.Mrc_Dist_Code_Combination_ID||CHR(9)||
				C1.Mrc_Base_Amount||CHR(9)||
				C1.Mrc_Base_Inv_Price_Variance||CHR(9)||
				C1.Mrc_Exchange_Rate_Variance||CHR(9)||
				C1.Mrc_Rate_Var_CCID||CHR(9)||
				C1.Mrc_Exchange_Date||CHR(9)||
				C1.Mrc_Exchange_Rate||CHR(9)||
				C1.Mrc_Exchange_Rate_Type||CHR(9)||
				C1.Mrc_Receipt_Conversion_Rate||CHR(9)||
				C1.Dist_Match_Type||CHR(9)||
					C1.RCV_Transaction_ID||CHR(9)||
				C1.Invoice_Distribution_ID||CHR(9)||
				C1.Parent_Reversal_ID||CHR(9)||
				C1.Tax_Recovery_Rate||CHR(9)||
				C1.Tax_Recovery_Override_Flag||CHR(9)||
				C1.Tax_Recoverable_Flag||CHR(9)||
				C1.Tax_Code_Override_Flag||CHR(9)||
				C1.Tax_Code_ID||CHR(9)||
				C1.PA_CC_AR_Invoice_ID||CHR(9)||
				C1.PA_CC_AR_Invoice_Line_Num||CHR(9)||
				C1.PA_CC_Processed_Code||CHR(9)||
				C1.Merchant_Document_Number||CHR(9)||
				C1.Merchant_Name||CHR(9)||
				C1.Merchant_Reference||CHR(9)||
				C1.Merchant_Tax_Reg_Number||CHR(9)||
				C1.Merchant_Taxpayer_ID||CHR(9)||
				C1.Country_Of_Supply||CHR(9)||
				C1.Matched_Uom_Lookup_Code||CHR(9)||
				C1.GMS_Burdenable_Raw_Cost||CHR(9)||
					C1.Accounting_Event_ID||CHR(9)||		-- ???
				C1.Prepay_Distribution_ID||CHR(9)||
				C1.Credit_Card_Trx_ID||CHR(9)||
				C1.Upgrade_Posted_Amt||CHR(9)||
				C1.Upgrade_Base_Posted_Amt||CHR(9)||
				C1.Inventory_Transfer_Status||CHR(9)||
				C1.Company_Prepaid_Invoice_ID||CHR(9)||
				C1.CC_Reversal_Flag||CHR(9)||
				C1.Prepay_Tax_Parent_ID||CHR(9)||
				C1.Awt_Withheld_Amt||CHR(9)||
				C1.Invoice_Includes_Prepay_Flag||CHR(9)||
				C1.Price_Correct_Inv_ID||CHR(9)||
				C1.Price_Correct_Qty||CHR(9)||
				C1.PA_CMT_Xface_Flag||CHR(9)||
				C1.Cancellation_Flag||CHR(9)||
				C1.Fully_Paid_Acctd_Flag||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Dist_Code_Combination_ID)||CHR(9)||
				C1.Invoice_Num||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetSOBShortName(C1.Set_Of_Books_ID)||CHR(9)||
				C1.Batch_Name||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)||CHR(9)||
				lv_PO_Num||CHR(9)||
				lv_PO_LineNum||CHR(9)||
				lv_PO_DistNum||CHR(9)||
				CAPBREXT_COMMON.GetInvoiceNumber(C1.Parent_Invoice_ID)||CHR(9)||
				C1.OU_Name||CHR(9)||
				lv_RCV_OrgName||CHR(9)||
				lv_RCV_RecNum||CHR(9)||
				lv_RCV_Qty
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such NGO exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;

END;
/